<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'


//定义数据模型
const loginData = ref({
    account: '',
    password: '',
    type: '',
    identifyCode: '',
})


// 动态标题
const dynamicTitle = computed(() => {
    switch (loginData.value.type) {
        case 'student':
            return '学生登录';
        case 'teacher':
            return '教师登录';
        case 'admin':
            return '管理员登录';
        default:
            return '登录';
    }
});



import { userLoginService } from '@/api/user.js'
import { getCaptchaService } from '@/api/user.js'



import { useTokenStore } from '@/stores/token.js'
import { useRouter } from 'vue-router'
const router = useRouter()
const tokenStore = useTokenStore();

// 调用登录接口
const login = async () => {
    try {
            let result = await userLoginService(loginData.value);    
            ElMessage.success(result.message);
            tokenStore.setToken(result.data); 
            router.push(result.url);        
    } catch (error) {      
        ElMessage.error('请求超时');
    }
}

const captchaUrl = ref('');
// 刷新验证码图片
const refreshCaptcha = async () => {
    try {
        const response = await getCaptchaService();
        const imageUrl = URL.createObjectURL(new Blob([response], { type: "image/jpeg" }));
        captchaUrl.value = imageUrl;
    } catch (error) {
        ElMessage.error('获取验证码失败，请稍后重试');
    }
};


refreshCaptcha();
</script>

<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <!-- 登录表单 -->
            <el-form ref="form" size="large" autocomplete="off" :model="loginData" :rules="rules">
                <!-- 动态标题 -->
                <el-form-item>
                    <h1 class="dynamic-title">{{ dynamicTitle }}</h1>
                </el-form-item>
                <!-- 用户角色选择 -->
                <div class="radio-group">
                    <label><input type="radio" value="teacher" v-model="loginData.type" /> 教师</label>
                    <label><input type="radio" value="student" v-model="loginData.type" /> 学生</label>
                    <label><input type="radio" value="admin" v-model="loginData.type" /> 管理员</label>
                </div>
                <!-- 用户名 -->
                <el-form-item prop="account">
                    <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginData.account"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="loginData.password"></el-input>
                </el-form-item>
                <!-- 验证码区域 -->
                <el-form-item prop="identifyCode">
                    <el-row :gutter="10">
                        <el-col :span="14">
                            <el-input placeholder="请输入验证码" v-model="loginData.identifyCode"></el-input>
                        </el-col>
                        <el-col :span="10">
                            <img :src="captchaUrl" alt="验证码" class="captcha-img" @click="refreshCaptcha" />
                        </el-col>
                    </el-row>
                </el-form-item>
                <!-- 记住我 -->
                <el-form-item class="flex">
                    <div class="flex">
                        <el-checkbox>记住我</el-checkbox>
                    </div>
                </el-form-item>
                <!-- 登录按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>



<style lang="scss" scoped>
/* 样式 */
.login-page {
    height: 100vh;
    background-color: #fff;

    .bg {
        background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
            url('@/assets/login_bg.jpg') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }

    .radio-group {
        display: flex;
        justify-content: space-between;
        margin-bottom: 20px;
    }

    .radio-group label {
        margin: 0 10px;
        font-size: 16px;
    }

    .dynamic-title {
        text-align: center;
        margin-bottom: 20px;
        font-size: 24px;
        font-weight: bold;
    }

    .captcha-img {
        width: 100%;
        cursor: pointer;
        border-radius: 5px;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>